﻿Imports System
Imports System.IO
Imports System.Collections.Generic
Imports System.Text.RegularExpressions
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Content
Imports System.Globalization

Namespace Processors

    Public Class OBJMesh

        Public Structure MaterialData
            Public MaterialName As String
            Public Ka As Vector3
            Public Kd As Vector3
            Public Ks As Vector3
            Public D As Single
            Public Ns As Single
            Public Illum As Single
            Public MapKa As String
            Public MapKd As String
            Public MapKs As String
            Public MapD As String
            Public MapNs As String
            Public MapBump As String
        End Structure

        Private ModObject() As MapObjects.Models.ModelObject
        Private G As GraphicsDeviceManager
        Private ContentDir As String
        Public ModelLocation As Vector3
        Public ModelTexture As Texture2D
        Private MTLFile As String
        Private VertBuffer As VertexBuffer
        Private IndBuffer As IndexBuffer
        Dim VertArray() As VertexPositionNormalTexture
        Dim IndArray() As Integer

        Public Function GetModelObjects() As MapObjects.Models.ModelObject()
            Return ModObject
        End Function

        Public Sub New(Graphics As GraphicsDeviceManager, ContentDirectory As String)
            G = Graphics
            ContentDir = ContentDirectory
        End Sub

        Public Sub ImportOBJFile(ModelName As String, Scale As Single)
            Dim objReader As System.IO.StreamReader
            Dim OBJLine As String
            Dim s() As String
            Dim v As Vector3
            Dim vt As Vector2
            Dim f() As String
            Dim FS As System.IO.FileStream
            Dim MO As New MapObjects.Models.ModelObject
            'Dim tmp() As Vector3
            Dim Positions = New List(Of Vector3)
            Dim Normals = New List(Of Vector3)
            Dim TexCoords = New List(Of Vector2)
            Dim ID As MapObjects.Models.IndexData
            Dim i As Integer
            Dim MOCt As Integer = -1
            Dim Textures() As Texture2D
            Dim Groups() As String
            Dim GrpCt As Integer = -1
            objReader = New System.IO.StreamReader(ContentDir & ModelName & ".mt2")
            Do While objReader.Peek <> -1
                GrpCt = GrpCt + 1
                OBJLine = objReader.ReadLine()
                s = Split(OBJLine, ",")
                ReDim Preserve Groups(GrpCt)
                ReDim Preserve Textures(GrpCt)
                Groups(GrpCt) = s(0)
                FS = New System.IO.FileStream(ContentDir & s(1), FileMode.Open)
                Textures(GrpCt) = Texture2D.FromStream(G.GraphicsDevice, FS)
                FS.Close()
            Loop
            objReader = Nothing
            objReader = New System.IO.StreamReader(ContentDir & ModelName & ".obj")
            Do While objReader.Peek <> -1
                OBJLine = objReader.ReadLine()
                OBJLine = Replace(OBJLine, "  ", " ")
                If Mid(OBJLine, 1, 1) = "#" Or UCase(Mid(OBJLine, 1, 1)) = "S" Then
                ElseIf UCase(Mid(OBJLine, 1, 6)) = "USEMTL" Then
                    s = Split(OBJLine, " ")
                    MO.TextureName = s(s.Count - 1)
                ElseIf UCase(Mid(OBJLine, 1, 6)) = "MTLLIB" Then
                    s = Split(OBJLine, " ")
                    MTLFile = s(s.Count - 1)
                ElseIf UCase(Mid(OBJLine, 1, 2)) = "VT" Then
                    s = Split(OBJLine, " ")
                    vt = New Vector2(CSng(s(1)), CSng(s(2)))
                    TexCoords.Add(vt)
                ElseIf UCase(Mid(OBJLine, 1, 2)) = "VN" Then
                    s = Split(OBJLine, " ")
                    v = New Vector3(CSng(s(1)), CSng(s(2)), CSng(s(3)))
                    Normals.Add(v)
                ElseIf UCase(Mid(OBJLine, 1, 1)) = "G" Then
                    s = Split(OBJLine, " ")
                    If MOCt = -1 Then
                    Else
                        If MO.Indices.Count = 0 Then
                            MOCt = MOCt - 1
                        Else
                            ModObject(MOCt) = MO
                        End If
                    End If
                    MOCt = MOCt + 1
                    ReDim Preserve ModObject(MOCt)
                    MO = New MapObjects.Models.ModelObject
                    MO.WorldMatrix = Matrix.Identity
                    MO.MaterialFileName = MTLFile
                    MO.Positions = Positions
                    MO.TexCoords = TexCoords
                    MO.Normals = Normals

                    MO.GroupName = Replace(Mid(OBJLine, 3, Len(OBJLine) - 2), " ", "_")
                    For i = 0 To Groups.Count - 1
                        If UCase(MO.GroupName) = UCase(Groups(i)) Then
                            MO.ModelTexture = Textures(i)
                            Exit For
                        End If
                    Next i
                    MO.Indices = New List(Of MapObjects.Models.IndexData)
                ElseIf UCase(Mid(OBJLine, 1, 1)) = "V" Then
                    s = Split(OBJLine, " ")
                    v = New Vector3(CSng(s(1)) * Scale, CSng(s(2)) * Scale, CSng(s(3)) * Scale)
                    Positions.Add(v)
                ElseIf UCase(Mid(OBJLine, 1, 1)) = "F" Then
                    s = Split(OBJLine, " ")
                    For i = s.Count - 1 To 1 Step -1
                        f = Split(s(i), "/")
                        If f.Count > 2 Then
                            ID.Vertex = CInt(f(0)) - 1
                            ID.TextCoord = CInt(f(1)) - 1
                            ID.Normal = CInt(f(2)) - 1
                        ElseIf f.Count > 1 Then
                            ID.Vertex = CInt(f(0)) - 1
                            ID.TextCoord = CInt(f(1)) - 1
                            ID.Normal = -999
                        Else
                            ID.Vertex = CInt(f(0)) - 1
                            ID.TextCoord = -999
                            ID.Normal = -999
                        End If
                        MO.Indices.Add(ID)
                    Next i
                End If
            Loop
            objReader = Nothing
            ModObject(MOCt) = MO
        End Sub

 
    End Class

End Namespace
